from celery_tasks.main import celery_app
from django.template import loader
from django.conf import settings
import os

# 导入商品分类菜单静态化方法
from goods.utils import get_categories
from goods.models import SKU


@celery_app.task(name="generate_static_list_search_html")
def generate_static_list_search_html(sku_id):
    """
    生成静态的商品列表页和搜索结果页html文件
    """
    # 商品分类菜单
    categories = get_categories()

    # 渲染模板,生成静态html文件
    context = {
        "categories": categories,
    }

    template = loader.get_template("list.html")
    html_text = template.render(context)
    file_path = os.path.join(settings.GENERATED_STATIC_HTML_FILES_DIR, "list.html")
    with open(file_path, "w") as f:
        f.write(html_text)


@celery_app.task(name="generate_static_sku_detail_html")
def generate_static_sku_detail_html(sku_id):
    """
    生成静态商品详情页面
    :param sku_id: 商品sku id
    :return:
    """
    # 商品分类菜单(主页,商品详情,列表页面都要做商品分类及菜单的静态化,所以封装出一个方法)
    categories = get_categories()

    # 获取当前sku的信息
    sku = SKU.objects.get(id=sku_id)
    sku.image = sku.skuimage_set.all()

    # 面包屑导航信息中的频道
    goods = sku.goods
    goods.channel = goods.category1.goodschannel_set.all()[0]

    # 构建当前商品的规格键
    # sku_key = [规格1参数id,规格2参数id,规格3参数id,...]
    sku_spesc = sku.skuspecification_set.order_by("spec_id")
    sku_key = []
    for spec in sku_spesc:
        sku_key.append(spec.option.id)

    # 获取当前商品的所有SKU
    skus = goods.sku_set.all()

    # 构造不同规格参数(选项)的sku字典
    # spec_sku_map = {
    #       (规格1参数id, 规格2参数id, 规格3参数id, ...):sku_id,
    #       (规格1参数id, 规格2参数id, 规格3参数id, ...):sku_id,
    #       ...
    #       }
    spec_sku_map = {}
    for s in skus:
        # 获取sku的规格参数
        s_specs = s.skuspecification_set.order_by("spec_id")
        # 用于形成规格参数-sku字典的键
        key = []
        for spec in s_specs:
            key.append(spec.option.id)
        # 向规格参数-sku字典添加记录
        spec_sku_map[tuple(key)] = s.id

    # 获取当前商品的规格信息
    specs = goods.goodsspecification_set.order_by("id")
    # 若当前sku的规格信息不完整,则不再继续
    if len(sku_key) < len(specs):
        return
    for index, spec in enumerate(specs):
        # 复制当前sku的规格键
        key = sku_key[:]
        # 该规格的选项
        options = spec.specificationoption_set.all()
        for option in options:
            # 在规格参数sku字典中查找符合当前规格的sku
            key[index] = option.id
            option.sku_id = spec_sku_map.get(tuple(key))

        spec.options = options

    # 渲染模板,生成静态html文件
    context = {
        "categories": categories,
        "goods": goods,
        "specs": specs,
        "sku": sku
    }

    # get_template默认从settings配置文件中找到TEMPLATE配置信息中的DIRS所指定的文件目录,从里面找detail.html文件
    template = loader.get_template("detail.html")
    # 使用查询出来的上下文数据,渲染detail.html页面
    html_text = template.render(context)

    # 先导入了16个商品详情文件,然后给每个文件都做了静态化处理,并保存在front_end_pc/goods/商品具体信息id.html中
    file_path = os.path.join(settings.GENERATED_STATIC_HTML_FILES_DIR, "goods/" + str(sku_id) + ".html")
    with open(file_path, "w") as f:
        f.write(html_text)
